package com.jiutong.meipengdai.views.dialog;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.jiutong.meipengdai.R;
import com.jiutong.meipengdai.views.wheelview.OnWheelChangedListener;
import com.jiutong.meipengdai.views.wheelview.WheelView;
import com.jiutong.meipengdai.views.wheelview.adapter.ArrayWheelAdapter;

/**
 * 作用:
 * Created by 78 on 2017/6/26.
 */

public class SexDialog extends DialogFragment implements OnWheelChangedListener, View.OnClickListener{
    private View mContainer;
    private View btnOk;
    private View btnCancel;
    private WheelView diretion;
    private String[] diracitons;
    private String currentDiraction;
    private SexDialog.OnDialogClickListener mListener;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //去掉dialog的标题，需要在setContentView()之前
        this.getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        Window window = this.getDialog().getWindow();
        //去掉dialog默认的padding
        window.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //设置dialog的位置在底部
        lp.gravity = Gravity.BOTTOM;
        //设置dialog的动画
        lp.windowAnimations = R.style.dialog_animation;
        window.setAttributes(lp);
        window.setBackgroundDrawable(new ColorDrawable());
        mContainer = inflater.inflate(R.layout.dialog_direction_select, null);

        initView();
        initEvent();

        updateFirstDiraction();

        return mContainer;
    }

    private void initView() {
        diretion = (WheelView) mContainer.findViewById(R.id.diretion);
        btnOk = mContainer.findViewById(R.id.btn_ok);
        btnCancel = mContainer.findViewById(R.id.btn_cancel);

    }

    private void initEvent() {
        diretion.addChangingListener(this);
        btnOk.setOnClickListener(this);
        btnCancel.setOnClickListener(this);
    }

    public void setOnDialogClickListener(SexDialog.OnDialogClickListener listener) {
        this.mListener = listener;
    }

    private void updateFirstDiraction() {

        diracitons = new String[2];
        diracitons[0] = "男";
        diracitons[1] = "女";
        diretion.setViewAdapter(new ArrayWheelAdapter<String>(getContext(), diracitons));
        currentDiraction = diracitons[0];
    }

    @Override
    public void onChanged(WheelView wheel, int oldValue, int newValue) {
        if (wheel == diretion) {
            currentDiraction = diracitons[newValue];
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_ok:
                mListener.onBtnClick(currentDiraction);
                dismiss();
                break;

            case R.id.btn_cancel:
                dismiss();
                break;
        }
    }

    public interface OnDialogClickListener {

        void onBtnClick(String diraction);
    }
}
